package com.shenzw.mybatisdemo.config;

import com.shenzw.mybatisdemo.aop.CheckParam;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * 参数校验器
 *
 * @author shenzw
 * @date 2023/12/8
 */
@Component
public class ParamConstraintValidation implements ConstraintValidator<CheckParam, Object> {

    private List<String> annotationValue;

    private String errorMessage;

    @Override
    public void initialize(CheckParam checkParam) {
        // 参数校验器上获取注解上的值
        annotationValue = Arrays.asList(checkParam.paramAllowValues());
        errorMessage = checkParam.message();
    }


    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (annotationValue.contains(value)) {
            return true;
        }

        // 传入参数不再规定参数列表中
        return false;
    }
}
